/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TreeEditorPanel.java
 *
 * Created on 11-May-2011, 10:24:57
 */
package net.neilcsmith.praxis.live.pxr;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import net.neilcsmith.praxis.live.pxr.api.RootProxy;
import net.neilcsmith.praxis.live.pxr.api.RootRegistry;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.Node;

/**
 *
 * @author Neil C Smith (http://neilcsmith.net)
 */
public class TreeEditorPanel extends javax.swing.JPanel implements ExplorerManager.Provider, PropertyChangeListener {

    private ExplorerManager manager;
    private PXRDataObject dob;

    /** Creates new form TreeEditorPanel */
    public TreeEditorPanel(PXRDataObject dob) {
        this.dob = dob;
        manager = new ExplorerManager();
        RootRegistry.getDefault().addPropertyChangeListener(this);
        checkRoot();
        initComponents();
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return manager;
    }


    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        checkRoot();
    }

    private void checkRoot() {
        RootProxy root = RootRegistry.getDefault().findRootForFile(dob.getPrimaryFile());
        if (root != null) {
            Node node = root.getNodeDelegate();
            if (node != manager.getRootContext()) {
                manager.setRootContext(node);
            }
        } else {
            manager.setRootContext(Node.EMPTY);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        view = new BeanTreeView();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(view, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(view, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane view;
    // End of variables declaration//GEN-END:variables

}
